home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / swtools / mipsABI / examples / sup / path.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  3.3 KB  |  97 lines

  1. /*
  2.  * Copyright (c) 1991 Carnegie Mellon University
  3.  * All Rights Reserved.
  4.  * 
  5.  * Permission to use, copy, modify and distribute this software and its
  6.  * documentation is hereby granted, provided that both the copyright
  7.  * notice and this permission notice appear in all copies of the
  8.  * software, derivative works or modified versions, and any portions
  9.  * thereof, and that both notices appear in supporting documentation.
  10.  *
  11.  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  12.  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  13.  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  14.  *
  15.  * Carnegie Mellon requests users of this software to return to
  16.  *
  17.  *  Software Distribution Coordinator   or   Software.Distribution@CS.CMU.EDU
  18.  *  School of Computer Science
  19.  *  Carnegie Mellon University
  20.  *  Pittsburgh PA 15213-3890
  21.  *
  22.  * any improvements or extensions that they make and grant Carnegie the rights
  23.  * to redistribute these changes.
  24.  */
  25. /*  path  --  break filename into directory and file
  26.  *
  27.  *  path (filename,direc,file);
  28.  *  char *filename,*direc,*file;
  29.  *  filename is input; direc and file are output (user-supplied).
  30.  *  file will not have any trailing /; direc might.
  31.  *
  32.  *  Note these rules:
  33.  *  1.  trailing / are ignored (except as first character)
  34.  *  2.  x/y is x;y where y contains no / (x may contain /)
  35.  *  3.  /y  is /;y where y contains no /
  36.  *  4.  y   is .;y where y contains no /
  37.  *  5.      is .;. (null filename)
  38.  *  6.  /   is /;. (the root directory)
  39.  *
  40.  * Algorithm is this:
  41.  *  1.  delete trailing / except in first position
  42.  *  2.  if any /, find last one; change to null; y++
  43.  *      else y = x;        (x is direc; y is file)
  44.  *  3.  if y is null, y = .
  45.  *  4.  if x equals y, x = .
  46.  *      else if x is null, x = /
  47.  *
  48.  *  HISTORY
  49.  * 20-Nov-79  Steven Shafer (sas) at Carnegie-Mellon University
  50.  *    Copied verbatim from PDP-11.  Still as messy as ever.
  51.  *    Some people have asked for a modification (I think that's a better
  52.  *    idea than a new routine) which will change the directory name
  53.  *    into an absolute pathname if it isn't one already.  The change
  54.  *    involves doing a getwd() and prepending that if appropriate, with
  55.  *    a "/" in between that and the directory part of the path.
  56.  *    If you want to be cute, you can also resolve ".."s at that time.
  57.  *
  58.  */
  59.  
  60. path (original,direc,file)
  61. char *original,*direc,*file;
  62. {
  63.     register char *y;
  64.     /* x is direc */
  65.     register char *p;
  66.  
  67.     /* copy and note the end */
  68.     p = original;
  69.     y = direc;
  70.     while (*y++ = *p++) ;        /* copy string */
  71.     /* y now points to first char after null */
  72.     --y;    /* y now points to null */
  73.     --y;    /* y now points to last char of string before null */
  74.  
  75.     /* chop off trailing / except as first character */
  76.     while (y>direc && *y == '/') --y;    /* backpedal past / */
  77.     /* y now points to char before first trailing / or null */
  78.     *(++y) = 0;                /* chop off end of string */
  79.     /* y now points to null */
  80.  
  81.     /* find last /, if any.  If found, change to null and bump y */
  82.     while (y>direc && *y != '/') --y;
  83.     /* y now points to / or direc.  Note *direc may be / */
  84.     if (*y == '/') {
  85.         *y++ = 0;
  86.     }
  87.  
  88.     /* find file name part */
  89.     if (*y)  strcpy (file,y);
  90.     else     strcpy (file,".");
  91.  
  92.     /* find directory part */
  93.     if (direc == y)        strcpy (direc,".");
  94.     else if (*direc == 0)  strcpy (direc,"/");
  95.     /* else direc already has proper value */
  96. }
  97.